<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<body>
    <div id="app">
        <div>
            <button @click="Delete">删除</button>
        </div>
        <former-bro></former-bro>
        <latter-bro></latter-bro>
    </div>

    <script>
        // Vue.component('div-child',{
        //     props: ['title'],
        //     data: function(){
        //             return {
        //                 cmsg: '子组件的数据'
        //             }
        //     },
        //     template: `
        //     <div>
        //         <div>{{ cmsg + "-----" + title }}</div>
        //         <button @click="$emit('enlarge-text')">增大字体大小</button>
        //     </div>
        //     `
        // });
        var Hub = new Vue();
        Vue.component('former-bro',{
            data: function(){
                return {
                    num: 0
                }
            },
            template: `
            <div>
                <div>James:{{ num }}</div>
                <div>
                    <button @click="handle">点击</button>
                </div>
            </div>
            `,
            methods: {
                handle: function(){
                     Hub.$emit('latter-event',1);
                }
            },
            mounted: function(){
                Hub.$on('former-event',(val) => {
                    this.num += val;
                });
            }
        })
        Vue.component('latter-bro',{
            data: function(){
                return {
                    num: 0
                }
            },
            template: `
            <div>
                <div>Bend:{{ num }}</div>
                <div>
                    <button @click="handle">点击</button>
                </div>
            </div>
            `,
            methods: {
                handle: function(){
                    Hub.$emit('former-event',2);
                }
            },
            mounted: function(){
                Hub.$on('latter-event',(val) => {
                    this.num += val;
                });
            }
        })
        var vm = new Vue({
            el: '#app',
            data: {
                msg: '父组件的数据',
                font: 10
            },
            methods: {
                enlarge: function(){
                    this.font += 4;
                },
                Delete: function(){
                    Hub.$off('former-event');
                }
            }
        })
    </script>
</body>
</html>